﻿using System;
using System.Linq;
using Migration.Models;
using System.Collections.Generic;
namespace Migration
{
    public partial class ClientEntities
    {
        private RealestateDataContext dataContext = new RealestateDataContext();

        public void Add(Client item)
        {
            dataContext.Clients.InsertOnSubmit(item);
            dataContext.SubmitChanges();
        }

        public void Update(Client item)
        {
            var updateItem = (from c in dataContext.Clients where c.ClientId == item.ClientId select c).FirstOrDefault();
            dataContext.SubmitChanges();
        }

        public void Delete(int id)
        {
            var item = (from c in dataContext.Clients where c.ClientId == id select c).FirstOrDefault();
            dataContext.Clients.DeleteOnSubmit(item);
            dataContext.SubmitChanges();
        }

        public Client GetById(int id)
        {
            var item = (from c in dataContext.Clients where c.ClientId == id select c).FirstOrDefault();
            return item;
        }

        public List<Client> GetAll()
        {
            return dataContext.Clients.OrderByDescending(c => c.ClientId).ToList();
        }

        public List<Client> GetByUserId(Guid userId)
        {
            return dataContext.Clients.Where(o => o.UserId.Equals(userId)).ToList();
        }
    }
}